<?php

namespace App\Http\Controllers\Admin;

use App\Services\Wxtj\WxtjUpstream;
use Illuminate\Http\Request;

/**
 * 微信消息分析统计
 * @package App\Http\Controllers\Admin
 */
class WxtjUpstreamController extends BaseWxController
{
    /**
     * 消息发送概况数据
     * @param Request $request
     */
    public function msg(Request $request, WxtjUpstream $wxtjUpstream)
    {
        if ($request->ajax()) {
            //关注渠道
            $query = \App\Models\WxtjUpstreamMsg::select('*');
            //查询指定微信应用
            $curWxMp = \Auth::user()->getWxMpCurrent();
            $appid = $curWxMp ? $curWxMp['appid'] : '';
            $query->where('appid', $appid);
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $startTime = $startTime ? date('Y-m-d', strtotime($startTime)) : date('Y-m-d', strtotime('-7 days'));
            $endTime = $request->get('end_time', '');
            $endTime = $endTime ? date('Y-m-d', strtotime($endTime)) : date('Y-m-d', strtotime('-1 days'));
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('ref_date', [$startTime, $endTime]);
            } elseif (!empty($startTime) && empty($endTime)) {
                $query->whereRaw("ref_date >= '" . $startTime . "'");
            } elseif (empty($startTime) && !empty($endTime)) {
                $query->whereRaw("ref_date <= '" . $endTime . "'");
            }
            //消息类型
            $msgType = $request->input('msg_type', 0);
            if ($msgType) {
                $query->where('msg_type', $msgType);
            }
            $rows = $query->get();
            $rows = $rows ? $rows->toArray() : [];
            $formatData = formatChartDataByDate($rows, 'ref_date', ['msg_user','msg_count'], $startTime, $endTime);
            return $formatData;
        }
        $this->viewData['msgTypeList'] = $wxtjUpstream->getMsgTypeList();
        $this->viewData['startDate'] = date('Y-m-d', strtotime('-7 days'));
        $this->viewData['endDate'] = date('Y-m-d', strtotime('-1 days'));
        return view('admin.wxtjupstream.msg', $this->viewData);
    }

    /**
     * 消息分送分时数据
     * @param Request $request
     */
    public function msghour(Request $request, WxtjUpstream $wxtjUpstream)
    {
        if ($request->ajax()) {
            //关注渠道
            $query = \App\Models\WxtjUpstreamMsghour::select('*');
            //查询指定微信应用
            $curWxMp = \Auth::user()->getWxMpCurrent();
            $appid = $curWxMp ? $curWxMp['appid'] : '';
            $query->where('appid', $appid);
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $startTime = $startTime ? date('Y-m-d', strtotime($startTime)) : date('Y-m-d', strtotime('-7 days'));
            $query->where('ref_date', $startTime);
            //消息类型
            $msgType = $request->input('msg_type', 0);
            if ($msgType) {
                $query->where('msg_type', $msgType);
            }
            $rows = $query->get();
            $rows = $rows ? $rows->toArray() : [];
            $formatData = formatChartDataByHour($rows, 'ref_hour', ['msg_user','msg_count']);
            return $formatData;
        }
        $this->viewData['msgTypeList'] = $wxtjUpstream->getMsgTypeList();
        $this->viewData['startDate'] = date('Y-m-d', strtotime('-1 days'));
        return view('admin.wxtjupstream.msghour', $this->viewData);
    }

    /**
     * 消息发送周数据
     * @param Request $request
     * @param WxtjUpstream $wxtjUpstream
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function msgweek(Request $request, WxtjUpstream $wxtjUpstream)
    {
        if ($request->ajax()) {
            //关注渠道
            $query = \App\Models\WxtjUpstreamMsgweek::select('*');
            //查询指定微信应用
            $curWxMp = \Auth::user()->getWxMpCurrent();
            $appid = $curWxMp ? $curWxMp['appid'] : '';
            $query->where('appid', $appid);
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $startTime = $startTime ? date('Y', strtotime($startTime)) : date('Y');
            $query->whereBetween('ref_date', [$startTime.'-01-01', $startTime.'-12-31']);
            //消息类型
            $msgType = $request->input('msg_type', 0);
            if ($msgType) {
                $query->where('msg_type', $msgType);
            }
            $rows = $query->get();
            $rows = $rows ? $rows->toArray() : [];
            $formatData = formatChartDataByWeek($rows, 'ref_date', ['msg_user','msg_count'], $startTime);
            return $formatData;
        }
        $this->viewData['msgTypeList'] = $wxtjUpstream->getMsgTypeList();
        $this->viewData['startDate'] = date('Y');
        return view('admin.wxtjupstream.msgweek', $this->viewData);
    }

    /**
     * 消息发送月数据
     * @param Request $request
     * @param WxtjUpstream $wxtjUpstream
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function msgmonth(Request $request, WxtjUpstream $wxtjUpstream)
    {
        if ($request->ajax()) {
            //关注渠道
            $query = \App\Models\WxtjUpstreamMsgmonth::select('*');
            //查询指定微信应用
            $curWxMp = \Auth::user()->getWxMpCurrent();
            $appid = $curWxMp ? $curWxMp['appid'] : '';
            $query->where('appid', $appid);
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $startTime = $startTime ? date('Y', strtotime($startTime)) : date('Y');
            $query->whereBetween('ref_date', [$startTime.'-01-01', $startTime.'-12-31']);
            //消息类型
            $msgType = $request->input('msg_type', 0);
            if ($msgType) {
                $query->where('msg_type', $msgType);
            }
            $rows = $query->get();
            $rows = $rows ? $rows->toArray() : [];
            $formatData = formatChartDataByMonth($rows, 'ref_date', ['msg_user','msg_count'], $startTime);
            return $formatData;
        }
        $this->viewData['msgTypeList'] = $wxtjUpstream->getMsgTypeList();
        $this->viewData['startDate'] = date('Y-m-d', strtotime('-1 days'));
        return view('admin.wxtjupstream.msgmonth', $this->viewData);
    }

    /**
     * 消息发送分布数据
     * @param Request $request
     * @param WxtjUpstream $wxtjUpstream
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function msgdist(Request $request, WxtjUpstream $wxtjUpstream)
    {
        if ($request->ajax()) {
            //关注渠道
            $query = \App\Models\WxtjUpstreamMsgdist::select('*');
            //查询指定微信应用
            $curWxMp = \Auth::user()->getWxMpCurrent();
            $appid = $curWxMp ? $curWxMp['appid'] : '';
            $query->where('appid', $appid);
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $startTime = $startTime ? date('Y-m-d', strtotime($startTime)) : date('Y-m-d', strtotime('-7 days'));
            $endTime = $request->get('end_time', '');
            $endTime = $endTime ? date('Y-m-d', strtotime($endTime)) : date('Y-m-d', strtotime('-1 days'));
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('ref_date', [$startTime, $endTime]);
            } elseif (!empty($startTime) && empty($endTime)) {
                $query->whereRaw("ref_date >= '" . $startTime . "'");
            } elseif (empty($startTime) && !empty($endTime)) {
                $query->whereRaw("ref_date <= '" . $endTime . "'");
            }
            //当日发送消息量分布的区间
            $countInterval = $request->input('count_interval', 0);
            $query->where('count_interval', $countInterval);

            $rows = $query->get();
            $rows = $rows ? $rows->toArray() : [];
            $formatData = formatChartDataByDate($rows, 'ref_date', ['msg_user'], $startTime, $endTime);
            return $formatData;
        }
        $this->viewData['countIntervalList'] = $wxtjUpstream->getCountIntervalList();
        $this->viewData['startDate'] = date('Y-m-d', strtotime('-7 days'));
        $this->viewData['endDate'] = date('Y-m-d', strtotime('-1 days'));
        return view('admin.wxtjupstream.msgdist', $this->viewData);
    }

    /**
     * 消息发送分布(周)数据
     * @param Request $request
     * @param WxtjUpstream $wxtjUpstream
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function msgdistweek(Request $request, WxtjUpstream $wxtjUpstream)
    {
        if ($request->ajax()) {
            //关注渠道
            $query = \App\Models\WxtjUpstreamMsgdistweek::select('*');
            //查询指定微信应用
            $curWxMp = \Auth::user()->getWxMpCurrent();
            $appid = $curWxMp ? $curWxMp['appid'] : '';
            $query->where('appid', $appid);
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $startTime = $startTime ? date('Y', strtotime($startTime)) : date('Y');
            $query->whereBetween('ref_date', [$startTime.'-01-01', $startTime.'-12-31']);
            //当日发送消息量分布的区间
            $countInterval = $request->input('count_interval', 0);
            $query->where('count_interval', $countInterval);

            $rows = $query->get();
            $rows = $rows ? $rows->toArray() : [];
            $formatData = formatChartDataByWeek($rows, 'ref_date', ['msg_user'], $startTime);
            return $formatData;
        }
        $this->viewData['countIntervalList'] = $wxtjUpstream->getCountIntervalList();
        $this->viewData['startDate'] = date('Y');
        return view('admin.wxtjupstream.msgdistweek', $this->viewData);
    }

    /**
     * 消息发送分布(月)数据
     * @param Request $request
     * @param WxtjUpstream $wxtjUpstream
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function msgdistmonth(Request $request, WxtjUpstream $wxtjUpstream)
    {
        if ($request->ajax()) {
            //关注渠道
            $query = \App\Models\WxtjUpstreamMsgdistmonth::select('*');
            //查询指定微信应用
            $curWxMp = \Auth::user()->getWxMpCurrent();
            $appid = $curWxMp ? $curWxMp['appid'] : '';
            $query->where('appid', $appid);
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $startTime = $startTime ? date('Y', strtotime($startTime)) : date('Y');
            $query->whereBetween('ref_date', [$startTime.'-01-01', $startTime.'-12-31']);
            //当日发送消息量分布的区间
            $countInterval = $request->input('count_interval', 0);
            $query->where('count_interval', $countInterval);

            $rows = $query->get();
            $rows = $rows ? $rows->toArray() : [];
            $formatData = formatChartDataByMonth($rows, 'ref_date', ['msg_user'], $startTime);
            return $formatData;
        }
        $this->viewData['countIntervalList'] = $wxtjUpstream->getCountIntervalList();
        $this->viewData['startDate'] = date('Y-m-d', strtotime('-1 days'));
        return view('admin.wxtjupstream.msgdistmonth', $this->viewData);
    }
}
